﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using AutoLoL.Logic.Parsers;
using AutoLoL.Logic.Properties;
using AutoLoL.Logic.Utils;

namespace AutoLoL.Logic.Masteries.Importers
{
    public class MobafireMasteryImporter : IMasteryImporter
    {
        public string Name
        {
            get { return "MobaFire.com"; }
        }

        public BitmapSource Icon
        {
            get { return ImageParser.BitmapToBitmapSource(Resources.mobafire); }
        }

        public bool Select(string input)
        {
            return input.ToLower().Contains("mobafire.com");
        }

        public string Import(string source)
        {
            try
            {
                source = source.ToLower();

                string plannerPrefix = "mobafire.com/league-of-legends/mastery-tree-planner#&tree1=";

                if (source.Contains(plannerPrefix) && source.Contains("&tree2=") && source.Contains("&tree3="))
                {
                    source = source.Substring(source.IndexOf(plannerPrefix) + plannerPrefix.Length);
                    source = source.Replace("&tree2=", string.Empty);
                    source = source.Replace("&tree3=", string.Empty);
                    source = source.Replace("-", string.Empty);

                    return source;
                }
                else if (source.Contains("mobafire.com/league-of-legends/build/"))
                {
                    // Get the html text
                    string text = Web.GetWebText(source);

                    //---------Grab Summoners--------------
                    string prefix = "mobafire.com/images/summoner-spell/";
                    string sommoner1, sommoner2;

                    //Get the summoners section of the page
                    string sommoners = text.Substring(text.IndexOf("<div class=\"spell float-left\">"));
                    sommoners = sommoners.Substring(0, sommoners.IndexOf("</div>"));
                    //get the first sommoner spell
                    sommoners = sommoners.Substring(sommoners.IndexOf(prefix));
                    sommoner1 = sommoners.Substring(0, sommoners.IndexOf(".png\""));
                    sommoner1 = sommoner1.Replace(prefix, "");
                    //move up to the second spell
                    sommoners = sommoners.Substring(sommoners.IndexOf(".png\""));
                    //grab the second sommoner spell
                    sommoners = sommoners.Substring(sommoners.IndexOf(prefix));
                    sommoner2 = sommoners.Substring(0, sommoners.IndexOf(".png\""));
                    sommoner2 = sommoner2.Replace(prefix, "");

                    //---------Grab Masteries--------------
                    // Strip off all html we do not need to reduce possible parsing errors
                    int startIndex = text.IndexOf("<div id=\"champion-mastery\"");

                    if (startIndex >= 0)
                    {
                        text = text.Substring(startIndex);
                        int endIndex = text.IndexOf("<!--mastery-col-right-->");

                        if (endIndex >= 0)
                        {
                            text = text.Substring(0, endIndex);
                            text = text.Replace("&nbsp;", " ");

                            // Parse text
                            XDocument doc = XDocument.Parse(text);

                            // Get all divs that
                            IEnumerable<XElement> divElements = doc.Descendants("div").Where(c =>
                                // Have the mastery class
                                c.Attribute("class") != null && c.Attribute("class").Value.Equals("mastery-skill")
                                    // belong to a mastery row
                                && c.Parent.Attribute("class") != null && c.Parent.Attribute("class").Value.Equals("mastery-row self-clear")
                                    // have at least one child
                                && c.HasElements
                                    // does not contain an em-child
                                && c.Elements("em").Count() == 0).ToList();

                            string result = string.Empty;

                            // Enumerate all divs
                            foreach (XElement masteryElement in divElements)
                            {
                                // An element has a value if it contains a span
                                XElement span = masteryElement.Descendants("span").FirstOrDefault();
                                if (span != null)
                                {
                                    result += span.Value;
                                }
                                else
                                {
                                    result += "0";
                                }
                            }

                            if (result.Length > 0)
                                result = sommoner1 + ":" + sommoner2 + ":" + result;

                            return result;
                        }
                    }
                }
            }
            catch { if (Debugger.IsAttached) throw; }

            // In case of an error return null
            return null;
        }
    }
}
